package club.framework.web.application.sys.role.controller;

import club.framework.web.application.sys.role.entity.Role;
import club.framework.web.application.sys.role.service.IRoleService;
import club.framework.web.base.entity.BasePageReqEntity;
import club.framework.web.base.entity.Result;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @date 2017/9/29.
 * @Email:sunmch@163.com
 * @author: 念梓
 * des:
 */
@Controller
@RequestMapping("sys/role")
public class RoleController {

    private static final Logger log = LogManager.getLogger(RoleController.class);

    @Resource
    private IRoleService roleService;

    @RequestMapping("index")
    public String index(){
        return "application/sys/role/index";
    }


    @RequestMapping("add")
    @ResponseBody
    public Object add(Role role){
        Result result = new Result();
        try{
            roleService.save(role);
        }catch (Exception ex){
            log.error("保存角色: {},失败: {}",role,ex.getMessage());
            result.setCode(Result.ERROR_CODE);
            result.setMsg(ex.getMessage());
        }
        return result;
    }

    @RequestMapping("update")
    @ResponseBody
    public Object update(Role role){
        Result result = new Result();
        try{
            roleService.update(role);
        }catch (Exception ex){
            log.error("修改角色信息: {},失败：{}",role,ex.getMessage());
            result.setCode(Result.ERROR_CODE);
            result.setMsg(ex.getMessage());
        }
        return result;
    }

    @RequestMapping("deleterolebyid")
    @ResponseBody
    public Object deleteRoleById(int id){
        Result result = new Result();
        try{
            roleService.delete(id);
        }catch (Exception ex){
            log.error("删除角色id:{},删除失败: {}",id,ex.getMessage());
            result.setCode(Result.ERROR_CODE);
            result.setMsg(ex.getMessage());
        }
        return result;
    }

    @RequestMapping("loadpage")
    @ResponseBody
    public Object loadPage(@RequestBody BasePageReqEntity<Role> basePageReqEntity){
        return roleService.queryPage(basePageReqEntity);
    }

    @RequestMapping("getrolebyid")
    @ResponseBody
    public Object getRoleById(int id){
        Result result = new Result();
        try{
            result.setData(roleService.getObject(id));
        }catch (Exception ex){
            log.error("根据角色id:{},获取角色信息失败: {}",id,ex.getMessage());
            result.setMsg(ex.getMessage());
        }
        return result;
    }


}
